﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FinosLibrary;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace FinosServiceLibrary.Server {
    [MessageProcess]
    class AdminLogic:ServerLogic {
        private string newAppName;
        [MessageMethod]
        public void GetAppList(AdminClient client) {
            client.call("GotAppList",AppManager.appnames);
        }
        [MessageMethod]
        public void GetInstanceList(AdminClient client, string appname) {
            client.call("GotInstanceList", new Package(appname, AppManager.GetInstanceList(appname)));
        }
        [MessageMethod]
        public void GetAppClients(AdminClient client,string appname) {
            client.call("GotAppClients", AppManager.apps[appname].appclients);
        }
        [MessageMethod]
        public void CreateAppBegin(AdminClient client,string appname) {
            client.byteHead = "CreatingApp";
            newAppName = appname;
            if(!Directory.Exists(AppManager.AppDir+"/"+newAppName)){
                Directory.CreateDirectory(AppManager.AppDir+"/"+newAppName);
            }
        }
        [MessageMethod]
        public void CreatingApp(AdminClient client,Package p) {
            try {
                //AppManager.StopApp(newAppName);
                File.WriteAllBytes(AppManager.AppDir + "/" + newAppName + "/" + newAppName + ".dll", p.byteBody);
                if (!AppManager.apps.ContainsKey(newAppName)) {
                    AppManager.ListApp();
                    client.call("GotAppList", AppManager.appnames);
                }
            } catch (Exception e) {
                Logger.error(e.Message);
                client.call("ServiceInfo", e.Message);
            }
            client.byteHead = null;
        }
        [MessageMethod]
        public void DeleteApp(AdminClient client,string appname) {
            try {
                AppManager.DeleteApp(appname);
                client.call("GotAppList", AppManager.appnames);
            }catch(Exception e){
                client.call("ServiceInfo",e.Message);
            }
        }
        [MessageMethod]
        public void ReloadApp(AdminClient client, string appname) {
            AppManager.StopApp(appname);
            AppManager.StartupApp(appname);
        }
        [MessageMethod]
        public void LogEnable(AdminClient client, bool enable) {
            client.receiveServerLog = enable;
        }
        [MessageMethod]
        public void GetSDK(AdminClient client,Package p) {
            p.byteBody = File.ReadAllBytes(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "sdk.rar");
            client.call(p);
        }
        [MessageMethod]
        public void GetPropertyPage(AdminClient client, Package p) {
            p.byteBody = File.ReadAllBytes(AppManager.AppDir + "/" + p.body + "/" + p.body + ".dll");
            client.call(p);
        }
        [MessageMethod]
        public void PropertyPagePackage(AdminClient client, Package p) {
            Package pp = p.getBody<Package>();
            Package ppp = pp.getBody<Package>();
            string appName = pp.head;
            if (AppManager.apps.ContainsKey(appName)&&AppManager.apps[appName].appDomain==null) {
                AppManager.StartupApp(appName);
                if (!AppManager.apps[appName].loader.clients.Contains(client.id)) {
                    AppManager.apps[appName].loader.addClient(client.id);
                }
            }
            AppManager.apps[appName].loader.doLogic(ppp.ToJsonBytes(), client);
        }
        [MessageMethod]
        public void Restart(AdminClient client) {
            string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Restart.bat";
            File.WriteAllLines(path, new string[] { "taskkill /f /im FinosService.exe", AppDomain.CurrentDomain.SetupInformation.ApplicationBase + AppDomain.CurrentDomain.SetupInformation.ApplicationName });
            Process.Start(path);
        }
    }
}
